﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.TextMining.Properties;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class ExtractionComponent:WorkflowComponentBase
    {
        #region prop
        private ExtractionInstruction instruction;
        private ExtractionOutput output;

        public override InstructionBase Instruction
        {
            get { return instruction; }
            set { instruction =(ExtractionInstruction) value; }
        }

        public override ResultBase Output
        {
            get { return output; }
            set { output =(ExtractionOutput) value; }
        }

        /// <summary></summary>
        public int RunID
        {
            get
            {
                return this.instruction.RunID;
            }
            set
            {
                this.instruction.RunID = value;
            }
        }

        /// <summary></summary>
        public string Workstation
        {
            get
            {
                return this.instruction.Workstation;
            }
            set
            {
                this.instruction.Workstation = value;
            }
        }

        /// <summary></summary>
        public string OperatorName
        {
            get
            {
                return this.instruction.OperatorName;
            }
            set
            {
                this.instruction.OperatorName = value;
            }
        }

        /// <summary></summary>
        /// <summary></summary>
        public int[] FiscalYears
        {
            get
            {
                return this.instruction.FiscalYears;
            }
            set
            {
                this.instruction.FiscalYears = value;
            }
        }

        /// <summary></summary>
        public string[] ActivityCodes
        {
            get
            {
                return this.instruction.ActivityCodes;
            }
            set
            {
                this.instruction.ActivityCodes = value;
            }
        }

        /// <summary></summary>
        public string[] ApplTypeCodes
        {
            get
            {
                return this.instruction.ApplTypeCodes;
            }
            set
            {
                this.instruction.ApplTypeCodes = value;
            }
        }

        /// <summary></summary>
        public string[] DocTypeCodes
        {
            get
            {
                return this.instruction.DocTypeCodes;
            }
            set
            {
                this.instruction.DocTypeCodes = value;
            }
        }

        /// <summary></summary>
        public string LogFilePath
        {
            get
            {
                return this.instruction.LogFilePath;
            }
            set
            {
                this.instruction.LogFilePath = value;
            }
        }

        /// <summary></summary>
        public string ErrorFilePath
        {
            get
            {
                return this.instruction.ErrorFilePath;
            }
            set
            {
                this.instruction.ErrorFilePath = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string LocalDocRootFolderPath
        {
            get
            {
                return this.instruction.LocalDocRootFolderPath;
            }
            set
            {
                this.instruction.LocalDocRootFolderPath = value;
            }
        }
        #endregion

        /// <summary>
        /// ctor
        /// </summary>
        public ExtractionComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.Enumeration.ToString();
            this.ComponentName = "Enumeration extractor";
            this.Description = "Extract table data from progress report pdf files";
            this.ComponentIcon = Resources.adobe;
            this.ComponentImage = Resources.Form;
            
            this.instruction = new ExtractionInstruction(
                this.ComponentID, Guid.Empty, 0, Environment.MachineName,
                Environment.UserName, null, null, null, null, string.Empty,
                string.Empty, string.Empty);
            this.output=new ExtractionOutput(this.ComponentID,Guid.Empty,0,0,0,string.Empty,string.Empty);
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "RunID", this.RunID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Workstation", this.Workstation);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OperatorName", this.OperatorName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FiscalYears", IntUtil.ToString(this.FiscalYears));
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ActivityCodes", StringUtil.ToString(this.ActivityCodes));
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ApplTypeCodes", StringUtil.ToString(this.ApplTypeCodes));
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DocTypeCodes", StringUtil.ToString(this.DocTypeCodes));
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LogFilePath", this.LogFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ErrorFilePath", this.ErrorFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LocalDocRootFolderPath", this.LocalDocRootFolderPath);
            
            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                ExtractionComponent component = base.Instantiate(xNode) as ExtractionComponent;

                component.RunID = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "RunID", "0"));
                component.Workstation = XmlDataUtil.GetAttributeValue(xNode, "Workstation", "");
                component.OperatorName = XmlDataUtil.GetAttributeValue(xNode, "OperatorName", string.Empty);
                component.FiscalYears = IntUtil.ToArray(XmlDataUtil.GetAttributeValue(xNode, "FiscalYears", ""));
                component.ActivityCodes = StringUtil.Split(XmlDataUtil.GetAttributeValue(xNode, "ActivityCodes", ""));
                component.ApplTypeCodes = StringUtil.Split(XmlDataUtil.GetAttributeValue(xNode, "ApplTypeCodes", ""));
                component.DocTypeCodes =
                    StringUtil.Split(XmlDataUtil.GetAttributeValue(xNode, "DocTypeCodes", string.Empty));
                component.LogFilePath = XmlDataUtil.GetAttributeValue(xNode, "LogFilePath", "");
                component.ErrorFilePath = XmlDataUtil.GetAttributeValue(xNode, "ErrorFilePath", "");
                component.LocalDocRootFolderPath = XmlDataUtil.GetAttributeValue(xNode, "LocalDocRootFolderPath", "");

                return component;
            }
            else
                throw new Exception("Unable to instantiate email: invalid type specified in xml node");
        }
    }
}
